package cn.edu.zafu.bigdata.lyq;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.io.Serializable;

class Student implements Serializable {
    private String id;
    private String name;
    private String phone;

    // 构造函数
    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    // Getter 和 Setter 方法
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }


    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}
public class StudentManagementSystem1 {
    private List<Student> students = new ArrayList<>();
    private Map<String,Student> studentMap = new HashMap<>();
    private static final String FILE_NAME = "students.dat";
    private Scanner scanner = new Scanner(System.in);

    // 加载学生信息
    public void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            students = (List<Student>) ois.readObject();
            for (Student student : students) {
                studentMap.put(student.getId(), student);
            }
        } catch (FileNotFoundException e) {
            // 文件不存在时，初始化空列表
            students = new ArrayList<>();
            studentMap = new HashMap<>();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 保存学生信息
    public void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 显示菜单
    public void displayMenu() {
        System.out.println("======= 学生信息管理系统 =======");
        System.out.println("1. 添加学生信息");
        System.out.println("2. 检索学生信息");
        System.out.println("3. 更新学生信息");
        System.out.println("4. 删除学生信息");
        System.out.println("5. 退出程序");
        System.out.print("请输入操作编号 (1-5): ");
    }

    // 添加学生信息
    public void addStudent() {
        while (true) {
            System.out.print("请输入学号: ");
            String id = scanner.nextLine();
            if (studentMap.containsKey(id)) {
                System.out.println("学号已被占用，请重新输入！");
                continue;
            }
            System.out.print("请输入姓名: ");
            String name = scanner.nextLine();
            System.out.print("请输入电话: ");
            String phone = scanner.nextLine();

            Student student = new Student(id, name, phone);
            students.add(student);
            studentMap.put(id, student);
            System.out.println("学生信息添加成功！");
            break;
        }
    }

    // 检索学生信息
    public void retrieveStudent() {
        while (true) {
            System.out.print("请输入要检索的学生学号 (QUIT 退出): ");
            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("QUIT")) {
                break;
            }
            Student student = studentMap.get(input);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生！");
            }
        }
    }

    // 更新学生信息
    public void updateStudent() {
        while (true) {
            System.out.print("请输入要更新的学生学号 (QUIT 退出): ");
            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("QUIT")) {
                break;
            }
            Student student = studentMap.get(input);
            if (student != null) {
                System.out.print("请输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String phone = scanner.nextLine();

                student.setName(name);
                student.setPhone(phone);
                System.out.println("学生信息更新成功！");
            } else {
                System.out.println("未找到该学生！");
            }
        }
    }

    // 删除学生信息
    public void deleteStudent() {
        while (true) {
            System.out.print("请输入要删除的学生学号 (QUIT 退出): ");
            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("QUIT")) {
                break;
            }
            Student student = studentMap.remove(input);
            if (student != null) {
                students.remove(student);
                System.out.println("学生信息删除成功！");
            } else {
                System.out.println("未找到该学生！");
            }
        }
    }
    public static void main(String[] args) {
        StudentManagementSystem1 system = new StudentManagementSystem1();
        system.loadStudents(); // 加载学生信息

        int choice;
        do {
            system.displayMenu();
            choice = Integer.parseInt(system.scanner.nextLine());

            switch (choice) {
                case 1:
                    system.addStudent();
                    break;
                case 2:
                    system.retrieveStudent();
                    break;
                case 3:
                    system.updateStudent();
                    break;
                case 4:
                    system.deleteStudent();
                    break;
                case 5:
                    System.out.println("退出程序，保存所有修改。");
                    system.saveStudents(); // 保存学生信息
                    break;
                default:
                    System.out.println("无效的操作编号，请重新输入！");
            }
        } while (choice != 5);

        system.scanner.close();
    }
}